package com.wangsai.framework.supersecurity.example.cloud.client.filter;

import cn.hutool.extra.servlet.ServletUtil;
import com.wangsai.framework.supersecurity.core.UserRequest;
import com.wangsai.framework.supersecurity.core.context.chain.UserContextFilter;
import com.wangsai.framework.supersecurity.core.context.chain.UserContextFilterChain;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


/**
 * @author Mr.Wang
 * @description
 * @date 2021-08-31 17:14
 */
@Component
public class IpUserContextFilter implements UserContextFilter {

    @Override
    public void doFilter(UserRequest userRequest, UserContextFilterChain filterChain) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        userRequest.getUser().put("ip", ServletUtil.getClientIP(requestAttributes.getRequest()));
        userRequest.getUser().put("clientId", "super-security-cloud-client");
        filterChain.doFilter(userRequest, filterChain);
    }
}